package com.wsoft.core.config.mybaties;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wsoft.constant.CommonConstant;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.vo.LoginUserDetail;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动注入创建人、创建时间、修改人、修改时间
 *
 * @author: juiet
 * @date: 2024年08月25日 15:55
 */
@Slf4j
@Component
public class FillFiledHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUserDetail sysUser = LoginUserUtil.getUserInfo();
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createBy", String.class, sysUser != null ? String.valueOf(sysUser.getLoginUserVO().getId()) : null);
        this.strictInsertFill(metaObject, "deleted", Integer.class, CommonConstant.YESNO_NO);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUserDetail sysUser = LoginUserUtil.getUserInfo();
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateBy", String.class, sysUser != null ? String.valueOf(sysUser.getLoginUserVO().getId()) : null);
    }
}
